package com.hspedu.map_;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author Stringzhua
 * @Date 2024/3/12 21:38
 * description:
 */
@SuppressWarnings("all")
public class MapExercise {
    public static void main(String[] args) {
        Map hashMap = new HashMap();
        hashMap.put(1,new Employee("jack", 300000, 1));
        hashMap.put(2,new Employee("tom", 21000, 2));
        hashMap.put(3,new Employee("milan", 12000, 3));
        //遍历
        //遍历显示工资大于18000的员工
        //1.使用keySet-->增强for
        System.out.println("使用keySet-->增强for");
        Set keySet = hashMap.keySet();
        for (Object key :keySet) {
            //先获取value
            Employee employee =(Employee) hashMap.get(key);//返回的是employee
            if(employee.getSal()>18000){
                System.out.println(employee);
            }
        }

        //2.使用EntrySet-->迭代器
        //体现一个比较难的知识点
        System.out.println("使用EntrySet-->迭代器");
        Set entrySet = hashMap.entrySet();
        Iterator iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
//            System.out.println(entry.getClass());//HashMap$Node
            //通过entry取得key,value
            Employee employee=(Employee) entry.getValue();
            if(employee.getSal()>18000){
                System.out.println(employee);
            }
        }

    }
}
class Employee{
    private String name;
    private double sal;
    private int id;

    public Employee(String name, double sal, int id) {
        this.name = name;
        this.sal = sal;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", id=" + id +
                '}';
    }
}

